<?php
/**
 * User: 刘红伟
 * Email: 454303753@qq.com
 * File:wechataccesstoken.php
 */

class wechatOpenId{
    private  $appid;
    private  $secret;
    private $_errorno;
    private $_error;
    private $_code;
    public function __construct($appid,$secret,$code='')
    {
        $this->appid=$appid;
        $this->secret=$secret;
        $this->_code=$code;
    }

    public function get(){

     $url="https://api.weixin.qq.com/sns/jscode2session?appid=".$this->appid."&secret=".$this->secret."&js_code=".$this->_code."&grant_type=authorization_code";

      $json= file_get_contents($url);

      $result=json_decode($json,true);
        return $result;
     if(isset($result['errcode'])){
        $this->_errorno     =   $result['errcode'];
        $this->_error       =   $result['errmsg'];
         return false;
     }
    if(isset($result['openid']) && $result['session_key']){
        return $result;
    }
    }
    public function getErrno()
    {
        return $this->_errno;
    }

    public function getError()
    {
        return $this->_error;
    }
    public  function curl_get($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        return $output;
    }
}